package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        String queryString = queryPageBean.getQueryString();
        Page<Menu> menus = menuDao.selectByCondition();
        long total = menus.getTotal();
         List<Menu> result = menus.getResult();

        //List<Menu> result=  menuDao.findAll();
        List<Map<String,Object>> inList = new ArrayList<>();

        if(result!=null && result.size()>0){
            for (Menu menu : result) {
                if(menu.getParentMenuId() == null){
                    String name = menu.getName();
                    Map<String,Object> map = new HashMap<>();
                    map.put("fMenu",name);
                    List<Menu> findMenuById =  menuDao.findMenuById(menu.getId());
                    List<Map<String, Object>> list = new ArrayList<>();

                    if(findMenuById!=null && findMenuById.size()>0){
                        for (Menu menu1 : findMenuById) {
                            Map<String,Object> objectMap = new HashMap<>();
                            String menu1Name = menu1.getName();
                            objectMap.put("cMenuName",menu1Name);
                            list.add(objectMap);
                        }

                        map.put("cMenus",list);
                    }

                    inList.add(map);


                }
            }
        }



        return new PageResult(total,inList);
    }
}
